package thinkingJava.chapter14;

import java.lang.reflect.Field;

class WithPrivateFinalField {
	private int i = 1;
	private final String s = "I am totally safe";
	private String s2 = "Am i safe?";
	public String toString() {
		return "i = " + i + " , s = " + s + " , s2 =  " + s2;
	}
}

public class ModifyingPrivateFields {
	public static void main(String[] args) throws Exception {
		WithPrivateFinalField wpff = new WithPrivateFinalField();
		System.out.println(wpff);
		Field f = wpff.getClass().getDeclaredField("i");
		f.setAccessible(true);
		System.out.println(f.getInt(wpff));
		f.setInt(wpff, 38);
		System.out.println(f.getInt(wpff));
		System.out.println(wpff);
		f = wpff.getClass().getDeclaredField("s");
		f.setAccessible(true);
		System.out.println(f.get(wpff));
		f.set(wpff, "arOh, change?!");
		System.out.println(f.get(wpff));
		System.out.println(wpff);
		f = wpff.getClass().getDeclaredField("s2");
		f.setAccessible(true);
		System.out.println(f.get(wpff));
		f.set(wpff, "no ,you're not safe");
		System.out.println(f.get(wpff));
		System.out.println(wpff);
	}
}
